package cn.iocoder.yudao.module.fengdong.smartelderly.controller.app.buyrecord;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.buyrecord.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.convert.buyrecord.BuyRecordConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.buyrecord.BuyRecordDO;
import cn.iocoder.yudao.module.fengdong.smartelderly.service.buyrecord.BuyRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 购买记录")
@RestController
@RequestMapping("/fengdong.smartelderly/buy-record")
@Validated
public class AppBuyRecordController {

    @Resource
    private BuyRecordService buyRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建购买记录")
    public CommonResult<Long> createBuyRecord(@Valid @RequestBody BuyRecordCreateReqVO createReqVO) {
        return success(buyRecordService.createBuyRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新购买记录")
    public CommonResult<Boolean> updateBuyRecord(@Valid @RequestBody BuyRecordUpdateReqVO updateReqVO) {
        buyRecordService.updateBuyRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除购买记录")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteBuyRecord(@RequestParam("id") Long id) {
        buyRecordService.deleteBuyRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得购买记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BuyRecordRespVO> getBuyRecord(@RequestParam("id") Long id) {
        BuyRecordDO buyRecord = buyRecordService.getBuyRecord(id);
        return success(BuyRecordConvert.INSTANCE.convert(buyRecord));
    }

    @GetMapping("/list")
    @Operation(summary = "获得购买记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<BuyRecordRespVO>> getBuyRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<BuyRecordDO> list = buyRecordService.getBuyRecordList(ids);
        return success(BuyRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得购买记录分页")
    public CommonResult<PageResult<BuyRecordRespVO>> getBuyRecordPage(@Valid BuyRecordPageReqVO pageVO) {
        PageResult<BuyRecordDO> pageResult = buyRecordService.getBuyRecordPage(pageVO);
        return success(BuyRecordConvert.INSTANCE.convertPage(pageResult));
    }

}
